﻿
using AntdUI;
using DABLL;
using DADAL;

namespace DataAcquisition
{
    public partial class TemplateManageForm : BaseForm
    {
        #region 属性
        /// <summary>
        /// key
        /// </summary>
        private string EncryptionKey = SqlSugarHelper.EncryptionKey;

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 10;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;
        #endregion

        #region 自定义方法
        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            Table1.Height = Height - Header.Height - StackPanel1.Height - Divider1.Height - Divider2.Height - Pagination1.Height - 5;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","模板编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipePosition","管道位置",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("FirstStage","第一阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("SecondStage","第二阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ThirdStage","第三阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("IntensityStage","强度阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RigorStage","严密阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Operation","操作",AntdUI.ColumnAlign.Center)
            };

        }
        /// <summary>模板列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<TemplateModel> list = Template.TemplateList(PageNumber, PageSize, ref TotalPages);
            UpdateTable(list);
            Pagination1.Total = TotalPages;
            Pagination1.PageSize = PageSize;
        }

        /// <summary>更新Table
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<TemplateModel> list)
        {
            var antItemList = new List<TemplateItem>();
            foreach (TemplateModel item in list)
            {
                string first = string.Empty;
                if (item.FirstStagePressure > 0 && item.FirstStageTime > 0)
                {
                    string timeUnit = item.FirstStageTimeUnit;
                    first = string.Format("压力:{0}Mpa--时长：{1}{2}", item.FirstStagePressure.ToString(), item.FirstStageTime.ToString(), timeUnit);
                }

                string second = string.Empty;
                if (item.SecondStagePressure > 0 && item.SecondStageTime > 0)
                {
                    string timeUnit = item.SecondStageTimeUnit;
                    second = string.Format("压力:{0}Mpa--时长：{1}{2}", item.SecondStagePressure.ToString(), item.SecondStageTime.ToString(), timeUnit);
                }

                string third = string.Empty;
                if (item.ThirdStagePressure > 0 && item.ThirdStageTime > 0)
                {
                    string timeUnit = item.ThirdStageTimeUnit;
                    third = string.Format("压力:{0}Mpa--时长：{1}{2}", item.ThirdStagePressure.ToString(), item.ThirdStageTime.ToString(), timeUnit);
                }

                string intensity = string.Empty;
                if (item.IntensityStagePressure > 0 && item.IntensityStageTime > 0)
                {
                    string timeUnit = item.IntensityStageTimeUnit;
                    intensity = string.Format("压力:{0}Mpa--时长：{1}{2}", item.IntensityStagePressure.ToString(), item.IntensityStageTime.ToString(), timeUnit);
                }

                string rigor = string.Empty;
                if (item.RigorStagePressure > 0 && item.RigorStageTime > 0)
                {
                    string timeUnit = item.RigorStageTimeUnit;
                    rigor = string.Format("压力:{0}Mpa--时长：{1}h", item.RigorStagePressure.ToString(), item.RigorStageTime.ToString(), timeUnit);
                }

                string pipe_position = string.Empty;
                if (item.PipePosition is not null)
                {
                    pipe_position = item.PipePosition;
                }

                antItemList.Add(new TemplateItem(item.Number, pipe_position, item.DesignPressure, item.ErrorRange,
                    first, second, third, intensity, rigor));
            }
            Table1.DataSource = antItemList;
        }
        #endregion

        #region 系统方法
        public TemplateManageForm()
        {
            InitializeComponent();
            Header.Text = "试压模板管理";
            FullScreen = true;
        }

        private void TemplateManageForm_Load(object sender, EventArgs e)
        {
            SetupForm();
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
            CheckForIllegalCrossThreadCalls = false;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            this.BeginInvoke(new Action(() => {
                TemplateAddForm frm = new TemplateAddForm();
                frm.Owner = this;
                frm.ShowDialog();
            }));
        }

        private void Pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            PageNumber = e.Current;
            Task.Run(() =>
            {
                LoadList();
            });
        }

        private void Table1_CellButtonClick(object sender, AntdUI.TableButtonEventArgs e)
        {
            if (e.Record is TemplateItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    string msg = string.Format("您确定要删除【{0}】模板吗？", data.Number);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            Response response = Template.DeleteTemplate(data.Number);
                            if (response.Code != 200)
                            {
                                Error(response.Msg);
                            }
                            else
                            {
                                Success(response.Msg);
                                LoadList();
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
